package org.jiuling.validation.spring.handle;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.jiuling.basics.enums.RegularEnum;
import org.jiuling.basics.validation.ObjectVerify;
import org.jiuling.basics.validation.StringVerify;
import org.jiuling.validation.spring.annotation.PhoneNumber;

import java.util.regex.Pattern;

/**
 * 手机号校验
 * @author 紫灵
 * @date 2024年07月01日 22时27分29秒
 */
public class PhoneNumberValidator implements ConstraintValidator<PhoneNumber, String> {
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StringVerify.isBlank(value)) return true;
        return Pattern.compile(RegularEnum.CHINESE_PHONE_NUMBER.getRegexp()).matcher(value).matches();
    }
}
